// Course List
function getCourseList(){
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'addcourses',
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
			var course_list="";
			var action="";
			var count=1;
			course_list+='<table>'+
							'<tr>'+
							'<th>Sno</th>'+
							'<th>CourseName</th>'+
							'<th>Action</th>'+
							'</tr>';
			var obj = eval("(" + data.courseList + ")");
				$.each(obj, function (index,Object) {
				if(Object.status==1){
					action='<a href="javascript:void(0)" onclick = "editCourseFunction('+Object.course_id+')">Edit</a>';
					/*+'/'+'<a href="javascript:void(0);" onclick ="deleteCourseFunction('+Object.course_id+')>Delete</a>'*/
				}
					course_list+='<tr>'+
					'<td>'+count+'</td>'+
					'<td>'+Object.course_name+'</td>'+
					'<td>'+action+'</td>'+
					'</tr>';
					count++;
				});	
				course_list+='</table>';
				$('#course_list').html(course_list);
			}
		}
	});
}
//Get all courses

function getAllCourses(){
	var splitCourseId=getParameterByName('id');
	var splitId=splitCourseId.split('-')
	if(splitId[1]==""){
		var hid_course_id=$('#hid_course_id').val();
	}else{
		var hid_course_id=splitId[1];
	}
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'addcourses/'+'0',
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
			var viewCourse="";
			var obj = eval("(" + data.editCourse + ")");
				$.each(obj, function (index,Object) {
					if((Object.course_id)!=null){
						if(Object.course_id==hid_course_id){
							viewCourse+='<option onclick="getSubjectsFunction('+Object.course_id+')" value="'+Object.course_id+'" selected=selected>'+Object.course_name+'</option>';
							 $("#add").attr("value",'UPDATE');
						}else{
							viewCourse+='<option onclick="getSubjectsFunction('+Object.course_id+')" value="'+Object.course_id+'">'+Object.course_name+'</option>';
						}
						$('#course_name').html(viewCourse);
					}
				});	
			}
		}
	});
}

//Get all subjects
function getAllSubjects(){
	var splitSubjectId=getParameterByName('id');
	var splitId=splitSubjectId.split('-');
	if(splitId[0]==""){
		var hid_subject_id=$('#hid_subject_id').val();
	}else{
		var hid_subject_id=splitId[0];
	}
	if(splitId[1]==undefined){
		var hid_course_id=$('#hid_course_id').val();
		
	}else{
		var hid_course_id=splitId[1];
	}
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'addsubjects/'+hid_subject_id+'-'+'2'+'-'+hid_course_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
			var viewSubjects="";
			var viewCourses="";
			var obj = eval("(" + data.Subjectlist + ")");
				$.each(obj, function (index,Object) {
					if((Object.subject_id)!=null){
						if(Object.subject_id==hid_subject_id){
							viewSubjects+='<option value="'+Object.subject_id+'" selected=selected>'+Object.subject_name+'</option>';
							$("#add").attr("value",'UPDATE');
						}else{
							viewSubjects+='<option value="'+Object.subject_id+'">'+Object.subject_name+'</option>';
						}
						$('#subject_name').html(viewSubjects);
					}
				});	
			}
		}
	});
}
//course editCourseFunction
function editCourseFunction(course_id){
	window.location="addcourses.html?id="+course_id;
}

function courseEditFunction(){
	var course_id=getParameterByName('id');
	if(course_id==""){
		var hid_course_id=$('#hid_course_id').val();
	}else{
		var hid_course_id=course_id;
	}
	
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'addcourses/'+hid_course_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
				var obj = eval("(" + data.editCourse + ")");
				$.each(obj, function (index,Object) {
					$('#course_name').val(Object.course_name);
					$('#add').attr('value','UPDATE');
				});	
			}
		}
	});
}

//Technology List
function getTechnologyList(){
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'addtechnology',
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){	
				var techmology_list="";
				var count=1;
				techmology_list+='<table>'+
					'<tr>'+
					'<th>Sno</th>'+
					'<th>Technology Name</th>'+
					'<th>Action</th>'+
					'</tr>';
				var obj = eval("(" + data.technologyList + ")");
				$.each(obj, function (index,Object) {
					if(Object.status==1){
						action='<a href="javascript:void(0)" onclick = "edittechnologyFunction('+Object.technology_id+')">Edit</a>';
					}
					techmology_list+='<tr>'+
					'<td>'+count+'</td>'+
					'<td>'+Object.technology_name+'</td>'+
					'<td>'+action+'</td>'+
					'</tr>';
					count++;
				});	
				techmology_list+='</table>';
				$('#technology_list').html(techmology_list);
			}
		}
	});
}
// Edit technology function
function edittechnologyFunction(technologyId){
	window.location="addtechnology.html?id="+technologyId;
}
function editTechnology(){
	var technology_id=getParameterByName('id');	
	if(technology_id==""){
		var hid_technologyId=$('#hid_technology_id').val();
	}else{
		var hid_technologyId=technology_id;
	}
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'addtechnology/'+hid_technologyId,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
				var obj = eval("(" + data.editCourse + ")");
				$.each(obj, function (index,Object) {
					$('#technology_name').val(Object.technology_name);
					$('#tech_description').val(Object.technology_description);
					$('#display_Image').html('<img src="../images/'+Object.technology_image+'" size="100" width="50" >');
					$('#hid_display_image').val(Object.technology_image);
					$('#hid_technology_id').val(Object.technology_id);
				});	
				$("#add").attr("value",'UPDATE');
				$('#hid_tech_val').val('1');
			}
		}
	});
}
// Edit subject function
function editSubjectFunction(subjectId,courseId){
	window.location='addsubjects.html?id='+subjectId+'-'+'0'+'-'+courseId;
}

function editSubject(){
	var splitSubject_id=getParameterByName('id');
	var splitId=splitSubject_id.split('-');
	if(splitId[0]==""){
		var hid_subject_id=$('#hid_subject_id').val();
	}else{
		var hid_subject_id=splitId[0];
	}
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'addsubjects/'+hid_subject_id+'-'+'1'+'-'+splitId[2],
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
				var viewSubjects="";
				var obj = eval("(" + data.Subjectlist + ")");
				$.each(obj, function (index,Object) {
					$('#subject_name').val(Object.subject_name);
					$("#add").attr("value",'UPDATE');
					/*if((Object.course_id)!=null){
						if(Object.course_id==splitId[2]){
							viewSubjects+='<option  value="'+Object.course_id+'" selected=selected>'+Object.course_name+'</option>';

						}else{
							viewSubjects+='<option  value="'+Object.course_id+'">'+Object.course_name+'</option>';
						}
					}*/
				});	
				//$('#course_name').html(viewSubjects);
			}
		}
	});
}

//Edit Question

function editQuestionFunction(subject_id,course_id,question_id){
	window.location="addquestions.html?id="+subject_id+'-'+course_id+'-'+question_id;
}

function editQuestion(){
	var splitquestion_id=getParameterByName('id');
	var splitIds=splitquestion_id.split('-');
	var admin_id = JSON.parse(localStorage["admin_id"]);
	if(splitIds[2]==undefined){
		var hid_question_id=$('#hid_question_id').val();
	}else{
		var hid_question_id=splitIds[2];
	}
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'addquestions/'+hid_question_id+'-'+'0'+'-'+'0'+'-'+'0'+'-'+'0'+'-'+'0',
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
				var obj = eval("(" + data.questionsList + ")");
				$.each(obj, function (index,Object) {
					$('#question_name').val(Object.question_name);
					$('#option1').val(Object.option1);
					$('#option2').val(Object.option2);
					$('#option3').val(Object.option3);
					$('#option4').val(Object.option4);
					if(Object.correct_answer=='A'){
						$canswer='option1';
					}else if(Object.correct_answer=='B'){
						$canswer='option2';
					}else if(Object.correct_answer=='C'){
						$canswer='option3';
					}else {
						$canswer='option4';
					}
					$('#correct_answer').val($canswer);
					if((Object.level)!=null){
						var level="";
						for(var i=1;i<=5;i++){
							if(i==Object.level){
								level+='<option value="'+i+'" selected=selected>'+i+'</option>';
							}else{
								level+='<option value="'+i+'">'+i+'</option>';
							}
						}
						$('#level').html(level);
					}
					if((Object.year_of_experience)!=null){
						var options="";
						for(var i=1;i<=5;i++){
							if(i==Object.year_of_experience){
								options+='<option value="'+i+'" selected=selected>'+i+'</option>';
							}else{
								options+='<option value="'+i+'">'+i+'</option>';
							}
						}
						$('#year_of_experience').html(options);
					}
					$('#add').attr('value','UPDATE');
				});	
			}
		}
	});
}
function getParameterByName(name) {
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

function changePasswordFunction(){
	var flag=true;
	var admin_id = JSON.parse(localStorage["admin_id"]);
	var oldpassword=$("#old_password").val();
	var newpassword=$("#new_password").val();
	var confirmpassword=$("#confirm_password").val();
	
	if(oldpassword==""){
		$('#old_passwordError').html("Required");
		flag=false;
	}else{
		$('#old_passwordError').html("");
	}
	if(newpassword==""){
		$('#new_passwordError').html("Required");
		flag=false;
	}
	if(confirmpassword==""){
		$('#confirm_passwordError').html("Required");
		flag=false;
	}else if(newpassword!=confirmpassword){
		$('#confirm_passwordError').html("Confirm password is wrong");
		flag=false;
	}else{
		$('#confirm_passwordError').html("");
	}
	
	if(flag==true){
		$.ajax({
			type		:	'PUT',
			url			:  	webServiceUrl+'changepassword/'+admin_id,
			dataType	: 	"json",
			data        : {old_password:oldpassword,cnf_password:confirmpassword},
			success: function(data){
				if(data.value==1){
					$("#old_password").val("");
					$("#new_password").val("");
					$("#confirm_password").val("");
					$('#passwordSuccessfully').html("Password successfully changed");
				}else{
					$('#old_passwordError').html("Old password is wrong");
				}
			}
		});
	}
}
function getSubjectsFunction(courseId){
	//alert(courseId);return false;
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'ChangeCourse/'+courseId,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){
			var viewSubjects="";
			var obj = eval("(" + data.getsubjects + ")");
				$.each(obj, function (index,Object) {
					if((Object.subject_id)!=null){
						viewSubjects+='<option value="'+Object.subject_id+'">'+Object.subject_name+'</option>';
						$('#subject_name').html(viewSubjects);
					}
				});	
				
			}
		}
	});
}

function getInterviewQuestionsList(){
var type=getParameterByName("id");
	$.ajax({
		type		:	'PUT',
		url			:  	webServiceUrl+'addquestions/'+type,
		dataType	: 	"json",
		data		:	{type:type},
		success: function(data){
			if(data.value==1){	
				var intQuestList="";
				var count=1;
				intQuestList+='<table>'+
					'<tr>'+
					'<th>Sno</th>'+
					//'<th>CourseName</th>'+
					//'<th>SubjectName</th>'+
					'<th>QuestionName</th>'+
					'<th>Option1</th>'+
					'<th>Option2</th>'+
					'<th>Option3</th>'+
					'<th>Option4</th>'+
					'<th>Correct Answer</th>'+
					'<th>Level</th>'+
					'<th>Year Of Experience</th>'+
					'<th>Action</th>'+
					'</tr>';
				var obj = eval("(" + data.intQuestList + ")");
				$.each(obj, function (index,Object) {
					if(Object.status==1){
						action='<a href="javascript:void(0)" onclick = "editQuestionFunction('+Object.subject_id+','+Object.course_id+','+Object.question_id+')">Edit</a>';
					}
					intQuestList+='<tr>'+
					'<td>'+count+'</td>'+
					//'<td>'+Object.course_name+'</td>'+
					//'<td>'+Object.subject_name+'</td>'+						
					'<td>'+Object.question_name+'</td>'+					
					'<td>'+Object.option1+'</td>'+
					'<td>'+Object.option2+'</td>'+
					'<td>'+Object.option3+'</td>'+
					'<td>'+Object.option4+'</td>'+
					'<td>'+Object.correct_answer+'</td>'+
					'<td>'+Object.level+'</td>'+
					'<td>'+Object.year_of_experience+'</td>'+
					'<td>'+action+'</td>'+
					'</tr>';
					count++;
				});	
				intQuestList+='</table>';
				$('#intQuestList').html(intQuestList);
			}
		}
	});
}

function editUserinfoFunction(){
var user_id=getParameterByName("id");
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'registration/'+user_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){	
			$('#add').attr('value','UPDATE');
				var obj = eval("(" + data.data + ")");
				$.each(obj, function (index,Object) {
					$('#user_name').val(Object.user_name);
					$('#user_email').val(Object.user_email);
					$('#user_password').val(Object.user_password);
					$('#user_mobile').val(Object.user_mobile);
					$('#hid_user_id').val('1');
				});	
			}
		}
	});
}
function editAdmininfoFunction(){
var user_id=getParameterByName("id");
	$.ajax({
		type		:	'GET',
		url			:  	webServiceUrl+'registration/'+user_id,
		dataType	: 	"json",
		success: function(data){
			if(data.value==1){	
			$('#add').attr('value','UPDATE');
				var obj = eval("(" + data.data + ")");
				$.each(obj, function (index,Object) {
					$('#user_name').val(Object.user_name);
					$('#user_email').val(Object.user_email);
					$('#user_password').val(Object.user_password);
					$('#user_mobile').val(Object.user_mobile);
					$('#hid_user_id').val('1');
				});	
			}
		}
	});
}
function editUserFunction(user_id,user_type){
	if(user_type==1){
		window.location="addadmins.html?id="+user_id;
	}else{
		window.location="addusers.html?id="+user_id;
	}
	
}

//Adduser
function addUserFunction(type){
	if(type==0){
		window.location="addusers.html";
	}else{
		window.location="addadmins.html";
	}
}
//Registartion Form
function userAddFunction(type){
var admin_id=JSON.parse(localStorage["admin_id"]);
   	var flag=true;
	var phoneno 		= /^\d{10}$/; 
	var charactervalidation = /^[a-zA-Z\s]*$/;
	var username		=	$('#user_name').val();
	var useremail		=	$('#user_email').val();
	var userpassword	=	$('#user_password').val();
	var mobile			= 	$('#user_mobile').val();
	var hid_user_id			= 	$('#hid_user_id').val();
	if(username==""){
		$('#user_nameError').html("Required");
		flag=false;
	}/*else if(!username.match(charactervalidation)){
		$('#user_nameError').html("Characters only");
		flag=false;
	}*/else{
		$('#user_nameError').html('');
	}
	
	if(useremail==""){
	   $("#user_emailError").html("Required");
	   flag=false;   
	}else{
	    if(validateEmail(useremail)==0){
		  $("#user_emailError").html("In Valid Email");
		  flag=false; 
	    }else{
			  $("#user_emailError").html(""); 
			}
	} 
	
	if(userpassword==""){
	   $("#user_passwordError").html("Required");
	   flag=false;   
	}else{
	    $("#user_passwordError").html(""); 
	} 
	
	if(mobile==""){
		$('#user_mobileError').html("Required");
		flag=false;
	}else if(isNaN(mobile)){
		$('#user_mobileError').html("Numbers only");
		flag=false;
	}else if(!mobile.match(phoneno)){
		$('#user_mobileError').html("Invalid phonenumber");
		flag=false;
	}else{
		$('#user_mobileError').html('');
	}
	if(flag==true){
		$('#registrationLoader').html('<img src="images/ajax-loader.gif"/>');
		$.ajax({
			type		:	'POST',
			url			:  	webServiceUrl+'registration',
			dataType	: 	"json",
			data		:	{ admin_id:admin_id,user_name:username,user_email:useremail,user_password:userpassword,user_mobile:mobile, user_type_id:type ,hid_user_id:hid_user_id },
			success: function(data){
				$('#registrationLoader').html('');
				if(data.value==1){
					$('#user_emailError').html("");
					localStorage.setItem( 'user_id', JSON.stringify(data.UserId));			
					localStorage.setItem( 'user_email', JSON.stringify($('#user_email').val()));	
					if(type==2){
						window.location="userlist.html";
					}else{
						window.location="adminlist.html";
					}
					
				}else{
					$('#registrationLoader').html('');
					$('#user_emailError').html(data.data);
				}
			}
		});
	}
}
//Check Email validation
function emailValidation(){
	var user_email=$('#user_email').val();
	if(user_email!==''){
		if(validateEmail(user_email)==0)
		{
			$('#user_emailError').html("In Valid email");		
		}else{	
			$.ajax({
				type		:	'GET',
				url			:  	webServiceUrl+'login/'+user_email,
				dataType	: 	"json",
				success: function(data){
					if(data.value==1){
						$('#user_emailError').html(data.data);
						return false;
					}else{
						$('#user_emailError').html("");
					}
				}
			});
		}
  }
}
